package com.linewell.commom.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linewell.commom.dao.IFeedBackDao;
import com.linewell.commom.model.FeedBack;
import com.linewell.commom.service.IFeedBackService;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.utils.DateUtils;
import com.linewell.system.utils.HqlHelper;

/**
 * 实现
 */
@Service
public class FeedBackServiceImpl implements IFeedBackService {
	
	@Autowired
	private IFeedBackDao feedBackDao;
	
	@Override
	public boolean removeAll(String ids) {
		feedBackDao.removeAll(ids);
		return false;
	}


	@Override
	public boolean update(FeedBack f,Integer integer) {
		feedBackDao.updateAll(f,integer);
		return false;
	}

	@Override
	public DataGrid datagrid(PageHelper ph, FeedBack f,String begin_time,String end_time) {
		DataGrid dg = new DataGrid();
		List<FeedBack> cml = new ArrayList<FeedBack>();
		//用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		
		//注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer(
				"from FeedBack t where 1=1 ");
		
		//以下为查询条件
		if(f.getContent() != null && !"".equals(f.getContent())){
			String content = null;
			try {
				content = URLDecoder.decode(f.getContent(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("content", "%" + content + "%");
			hql.append(" and t.content like :content ");
		}
		
		if(f.getSysUsers() != null && !"".equals(f.getSysUsers())){
			
			if(f.getSysUsers().getUserId() == 9){
				hql.append(" and t.sysUsers.userId is not null ");
			}
			if(f.getSysUsers().getUserId() == 1){
				hql.append(" and t.sysUsers.userId is null ");
			}
			
		}
		
//		if(f.getFlag() != null && !"".equals(f.getFlag())){
//			params.put("flag", f.getFlag().byteValue());
//			hql.append(" and t.flag = :flag ");
//		}
		
		if (begin_time != null && !"".equals(begin_time)) {
			if (end_time != null && !"".equals(end_time)) {
				params.put("begin_time", DateUtils.getStringToDate(begin_time));
				params.put("end_time", DateUtils.getStringToDate(DateUtils.getDateAddDay(end_time)));
				hql.append(" and t.createTime >= :begin_time and t.createTime <= :end_time ");

			} else {
				params.put("begin_time", DateUtils.getStringToDate(begin_time));
				hql.append(" and t.createTime >= :begin_time ");

			}

		} else {
			if (end_time != null && !"".equals(end_time)) {
				params.put("end_time", DateUtils.getStringToDate(end_time));
				hql.append(" and t.createTime >= :end_time ");

			}
		}
		
		
		hql.append(" and t.status > 0 ");
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<FeedBack> l = feedBackDao.find(hql.toString(), params, ph.getPage(),
				ph.getRows());
		dg.setRows(l);
		dg.setTotal(feedBackDao.count("select count(*) " + hql, params));
		return dg;
	}


	@Override
	public boolean save(FeedBack c) {
		// TODO Auto-generated method stub
		feedBackDao.save(c);
		return true;
	}
	
	
	
	
}
